package com.wang.controller.api;

import com.wang.entity.User;
import com.wang.service.CartService;
import com.wang.utils.ConstantUtil;
import com.wang.utils.ResponseEnum;
import com.wang.utils.ResponseUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * @Classname ApiCartController
 * @Description TODO
 * @Date 2024/9/12 14:10
 * @Created by pc
 */
@RestController
@Slf4j
@RequestMapping("/api/cart")
@Api(value = "购物车",tags = "ApiCartController")
public class ApiCartController {
    @Resource
    private CartService cartService;

    @GetMapping("/findCartCount")
    @ApiOperation(value = "获取用户购物车数量",notes = "前台获取所有用户的购物车数量")
    public ResponseUtil findCartCount(HttpSession session){
       User loginUser =  (User)session.getAttribute(ConstantUtil.LOGIN_USER_KEY);
       ResponseUtil r = null;
       if(loginUser != null){
           r = cartService.findCountByUserId(loginUser.getId());
       }else{
           r = ResponseUtil.get(ResponseEnum.CART_FIND_COUNT_FAILURE,null);
       }
       return r;
    }
    @GetMapping("/findCart")
    @ApiOperation(value = "获取用户购物车",notes = "前台获取用户的购物车详情")
    public ResponseUtil findCart(HttpSession session){
        User loginUser = (User) session.getAttribute(ConstantUtil.LOGIN_USER_KEY);
        return cartService.findCartByUserId(loginUser.getId());
    }

    // 更新购物车状态  参数 id  数量 quantity checked
    @PostMapping("/updateCart")
    @ApiOperation(value = "更新用户购物车",notes = "前台更新用户的购物车详情")
    public ResponseUtil updateCart(
            @RequestParam("id") Integer id,
            @RequestParam("quantity") Integer quantity,
            @RequestParam("checked") Integer checked
    ){
        return cartService.updateCart(id,quantity,checked);
    }

    // 删除单个商品 根据id删除
    @PostMapping("/delCartById")
    @ApiOperation(value = "删除购物车",notes = "前台删除单个购物车")
    public ResponseUtil delCartById(@RequestParam("id")Integer id){
        return cartService.delCartById(id);
    }

    @PostMapping("/delCartByIds")
    @ApiOperation(value = "批量购物车",notes = "前台批量删除购物车商品")
    public ResponseUtil delCartByIds(@RequestParam("idArr")Integer[]idArr){
        return cartService.delCartByIds(idArr);
    }

    // 清空购物车
    @PostMapping("/delCartByUserId")
    @ApiOperation(value = "清空购物车",notes = "前台批量删除购物车商品")
    public ResponseUtil delCartByUserId(HttpSession session){
        User loginUser = (User) session.getAttribute(ConstantUtil.LOGIN_USER_KEY);
        return cartService.delCartByUserId(loginUser.getId());
    }

}
